<?php

function unsDepartement_addWorkPraticeGroups($name, $idTutorialGroup) {
    global $babDB;
    global $babBody;

    if (null != $name && null != $idTutorialGroup) {

        $tutorialGroup = unsDepartement_getTutorialGroup($idTutorialGroup);
        $groupID = bab_createGroup($name, " ", null, $tutorialGroup['groupID']); // on crée le groupe de la formation

        if (0 != $groupID) {// Si != 0 alors le groupe a été crée et ça retourne l'id du group
            $babDB->db_query("INSERT INTO ". UNSDEPARTEMENT_WORK_PRATICE_GROUPS_TABLE_NAME ." VALUES (null, '" . $name . "', " . $idTutorialGroup . ", " . $groupID . ", now(), " . $GLOBALS['BAB_SESS_USERID'] . ");");
        } else {
            $babBody->addError(bab_translate("A Work Pratice Group with this name all ready exist !", $GLOBALS['babAddonFolder']));
        }
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
}

function unsDepartement_editWorkPraticeGroup($id, $name)
{
    global $babDB, $babBody;

    $workPraticeGroup = unsDepartement_getWorkPraticeGroup($id);
    $groupID = $workPraticeGroup['groupID'];
    bab_updateGroup($groupID, $name, $description);
    $babDB->db_query("update " . UNSDEPARTEMENT_WORK_PRATICE_GROUPS_TABLE_NAME . " set workPraticeGroup_name ='" . $name . "' where id = " . $id);
}

function unsDepartement_getWorkPraticeGroupsForTutorialGroup($id) {
   global $babDB;

   $res = $babDB->db_query("select * from " . UNSDEPARTEMENT_WORK_PRATICE_GROUPS_TABLE_NAME . " where workPraticeGroup_tutorialGroupID = " . $id);

    $workPraticeGroups = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $workPraticeGroups[$i] = $arr;
        $i++;
    }
    return $workPraticeGroups; 
}

function unsDepartement_getWorkPraticeGroup($id) {
    global $babDB;
    
    $res = $babDB->db_query("select id, workPraticeGroup_name as name, workPraticeGroup_tutorialGroupID as tutorialGroupID, workPraticeGroup_groupID as groupID from " . UNSDEPARTEMENT_WORK_PRATICE_GROUPS_TABLE_NAME . " where id = " . $id);

    $workPraticeGroup = $babDB->db_fetch_array($res);

    return $workPraticeGroup;
}

function unsDepartement_deleteWorkPraticeGroup($id) {
    global $babDB, $babBody;
    
    
    $idWorkPraticeGroup = $id;
    $workPraticeGroup = unsDepartement_getWorkPraticeGroup($idWorkPraticeGroup);
    bab_removeGroup($workPraticeGroup['groupID']);
    $babDB->db_query("delete from " . UNSDEPARTEMENT_WORK_PRATICE_GROUPS_TABLE_NAME . " where id = " . $idWorkPraticeGroup . ";");

}
?>
